package cn.qyd.javathread;

/**
 * @author 邱运铎
 * @date 2024-03-14 16:24
 */
public class ThreadGroupDemo02 {
    public static void main(String[] args) {
        ThreadGroup threadGroup = new ThreadGroup("testGroup"){
            public void uncaughtException(Thread t, Throwable e) {
                System.out.println(t.getName() + ":" +e.getMessage());
            }
        };

        Thread thread = new Thread(threadGroup, () -> {
            throw new RuntimeException("测试异常");
        });
        threadGroup.setMaxPriority(3);
        //在某一线程组中的线程，设定其优先级只能小于等于线程组的最大优先级，超过会线程的优先级会被置为线程组的最大优先级
        thread.setPriority(6);

        thread.start();

        System.out.println(threadGroup.getMaxPriority());
        System.out.println(thread.getPriority());
        System.out.println(Thread.currentThread().getThreadGroup().getMaxPriority());
        System.out.println(Thread.currentThread().getPriority());

        for (int i = 1; i <= 10; i++) {
            Thread myThread = new Thread(() -> {
                System.out.println("当前线程： " + Thread.currentThread().getName() + "  : 优先级： " +Thread.currentThread().getPriority());
            });
            myThread.setName("myThread" + i);
            myThread.setPriority(i);
            myThread.start();
        }
    }
}
